//package com.videoaggre.zcl.api
//
//import com.alibaba.fastjson.JSON
//import com.alibaba.fastjson.JSONObject
//import com.videoaggre.zcl.common.Config
//import com.videoaggre.zcl.common.UserDataCache
//import com.videoaggre.zcl.utils.Logger
//import okhttp3.FormBody
//import okhttp3.Interceptor
//import okhttp3.MediaType.Companion.toMediaType
//import okhttp3.RequestBody.Companion.toRequestBody
//import okhttp3.Response
//
///**
// * Created by zhoucl on 2019/11/12 0012.
// */
//class SignInterceptor : Interceptor {
//
//    companion object {
//        const val PARAM = "PARAM"
//    }
//
//    @Throws(Exception::class)
//    override fun intercept(chain: Interceptor.Chain): Response {
//        var request = chain.request()
//        if ("POST" == request.method) {
//            val formBody = request.body as FormBody
//            val params = mutableMapOf<String, String>()
//            params["accesstoken"] = UserDataCache.getToken()
////            params["accesstoken"] = "ceac50983ca34786bceb0f233cc33bed"
//            for (i in 0 until formBody.size) {
//                if (formBody.name(i) != PARAM)
//                    params[formBody.name(i)] = formBody.value(i)
//            }
//            if (BuildConfig.DEBUG) Logger.e("${request.url}请求参数==>${JSON.toJSONString(params)}")
//            val encrypt = KFuPayDecryptUtil.encrypt(JSON.toJSONString(params), KFuPayDecryptUtil.getRandom(), Config.PASSWORD)
//            val paramJson = JSONObject()
//            paramJson["signData"] = MD5Encode(paramMd5(params)).toUpperCase()
//            paramJson["random"] = encrypt[0]
//            paramJson["encryptData"] = encrypt[1]
//            request = request.newBuilder().post(paramJson.toJSONString().toRequestBody("application/json;charset=utf-8".toMediaType())).build()
//        }else{
//            request = request.newBuilder().removeHeader("Accept-Encoding").build()
//            Logger.e("request===>${request.headers}")
//        }
//        return chain.proceed(request)
//    }
//
//    private fun paramMd5(params: MutableMap<String, String>): String {
//        val newParams = params.toSortedMap(compareBy { it })
//        val paramsSign = StringBuilder()
//        for (entry in newParams) {
//            if (entry.value.isNotEmpty() && entry.value != "null") {
//                paramsSign.append(entry.key).append("=").append(entry.value).append("&")
//            }
//        }
//        return paramsSign.append("md5key=").append(Config.MD5KEY).toString()
//    }
//}